package cn.bytecollege.filter;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Room;
import cn.bytecollege.entity.RoomCategory;
import cn.bytecollege.service.RoomCategoryService;
import cn.bytecollege.service.RoomService;
import com.google.code.kaptcha.Constants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 房间过滤器
 * YL
 */


@WebFilter(filterName = "RoomFilter",servletNames = {"RoomAddController","RoomUpdateController"})
public class RoomFilter implements Filter {
    RoomService roomService =new RoomService();
    RoomCategoryService roomCategoryService = new RoomCategoryService();
    String msg = null;

    public void init(FilterConfig config) throws ServletException{
    }

    public void destroy(){
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        String method = req.getMethod();
        //接受房间号和价格
        String number = request.getParameter("number");
        String price = request.getParameter("price");
        if(method.equalsIgnoreCase("post")){
            if(uri.contains("roomAdd")) {
                Pattern numberPattern = Pattern.compile("^\\d{3}$");
                Matcher numberMather = numberPattern.matcher(number);
                if (!numberMather.matches()) {
                    //房间号不合法
                    msg = "房间号是3位，只能包含数字";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/roomAdd.jsp").forward(request, response);
                    return;
                }
                if (!valid(request, response)) {
                    forward(request, response);
                    return;
                }
            }
//            }else
            if(uri.contains("roomUpdate")){
                String id = request.getParameter("id");
                Room room=new Room();
                room=roomService.findById(Integer.valueOf(id));
                request.setAttribute("room",room);
                List<RoomCategory> list=roomCategoryService.find();
                request.setAttribute("list",list);
                Pattern numberPattern = Pattern.compile("^\\d{3}$");
                Matcher numberMather = numberPattern.matcher(number);
                if(!numberMather.matches()){
                    //房间号不合法
                    msg = "房间号是3位，只能包含数字";
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/roomUpdate.jsp").forward(request,response);
                    return;
                }
                if(!valid(request,response)){
                    forward(request, response);
                    return;
                }
//                Room room = new Room();
//                String id = request.getParameter("id");
//                room.setRoomId(Integer.valueOf(id));
//                List<RoomCategory> list = roomCategoryService.find();
//                request.setAttribute("list",list);
//                request.setAttribute("msg",msg);
//                request.getRequestDispatcher(Resource.PAGE_LOCATION + "roomUpdate.jsp")
//                        .forward(request,response);

            }
            chain.doFilter(request,response);
            return;
        }
        chain.doFilter(request,response);

    }

    public void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException{
        List<RoomCategory> list = roomCategoryService.find();
        request.setAttribute("list",list);
        request.setAttribute("msg",msg);
        request.getRequestDispatcher(Resource.PAGE_LOCATION + "/roomAdd.jsp")
                .forward(request,response);
    }
    public boolean valid(ServletRequest request,ServletResponse response) throws ServletException,IOException{
        HttpServletRequest req = (HttpServletRequest) request;
        //获取参数
        //1.接收请求参数
        String number = req.getParameter("number");
        String categoryId = req.getParameter("category");
        String price = req.getParameter("price");
        if(number==null||number.equals("")){
            msg = "房间号不能为空！";
            forward(request,response);
            return false;
        }
        if(categoryId==null||categoryId.equals("")){
            msg = "房间类型不能为空！";
            forward(request,response);
            return false;
        }
        if(price==null||price.equals("")){
            msg = "房间价格不能为空！";
            forward(request,response);
            return false;
        }
        return true;
    }
}
